package com.qm.hibernate;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;


/**
 * A data access object (DAO) providing persistence and search support for Tpost
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.qm.hibernate.Tpost
 * @author MyEclipse Persistence Tools
 */

public class TpostDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TpostDAO.class);

	public void save(Tpost transientInstance) {
		log.debug("saving Tpost instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Tpost persistentInstance) {
		log.debug("deleting Tpost instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Tpost findById(java.lang.Integer id) {
		log.debug("getting Tpost instance with id: " + id);
		try {
			Tpost instance = (Tpost) getSession().get("com.qm.hibernate.Tpost",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Tpost instance) {
		log.debug("finding Tpost instance by example");
		try {
			List results = getSession()
					.createCriteria("com.qm.hibernate.Tpost")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Tpost instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Tpost as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Tpost instances");
		try {
			String queryString = "from Tpost";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Tpost merge(Tpost detachedInstance) {
		log.debug("merging Tpost instance");
		try {
			Tpost result = (Tpost) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Tpost instance) {
		log.debug("attaching dirty Tpost instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Tpost instance) {
		log.debug("attaching clean Tpost instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List  findBySection(String section,int begin ,int len) {
		// TODO Auto-generated method stub
		Session s = getSession();
		s.beginTransaction();		
		String hql = " from Tpost p,Tuser u where section = ? and u.id = p.tuser.id";		
		Query q = s.createQuery(hql);
		q.setString(0, section);		
		if (len > 0)
		{
		q.setFirstResult(begin);
		q.setMaxResults(len);
		}
		List<?> list = q.list();				
		s.getTransaction().commit();
		s.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public void delPost(Integer id)
	{
		String sql = "select * from tpost where authorid = ?";
		String sql2 = "select * from tpostcomment where postid = ?";
		Integer tempId;
		TpostDAO pd = new TpostDAO();
		TpostcommentDAO pcd= new TpostcommentDAO();
		Session s = getSession();
		s.beginTransaction();	
		List<Tpost> l = s.createSQLQuery(sql).addEntity(Tpost.class).setInteger(0, id).list();
		List <Tpostcomment> l2;
		System.out.println("happy middle1!");
		for(int i = 0;i < l.size();i++)
		{
			Tpost tempPost = (Tpost) l.get(i);
			tempId = tempPost.getId();
			l2 = (List<Tpostcomment>)s.createSQLQuery(sql2).addEntity(Tpostcomment.class).setInteger(0, tempId).list();
			for(int j = 0;j < l2.size();j++)
			{
				pcd.delete(l2.get(j));
			}
			pd.delete(tempPost);
		}
		s.getTransaction().commit();
		s.close();
	}
	
	
}